package com.ruoyi.project.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.common.utils.file.MinioUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.survey.domain.entity.Question;
import com.ruoyi.project.survey.domain.param.QuestionAdd;
import com.ruoyi.project.survey.domain.param.QuestionQuery;
import com.ruoyi.project.survey.mapper.QuestionMapper;
import com.ruoyi.project.survey.service.QuestionService;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问卷表(Question)表服务实现类
 *
 * @author makejava
 * @since 2024-03-17 08:41:42
 */
@Service("questionService")
@RequiredArgsConstructor
public class QuestionServiceImpl implements QuestionService {
    private final QuestionMapper questionMapper;
    private final MinioUtil minioUtil;

    @Override
    public AjaxResult save(QuestionAdd add) {
        Question question=new Question();
        BeanUtils.copyProperties(add,question);
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i < add.getContents().size(); i++) {
            buffer.append((i+1)+"."+add.getContents().get(i));
            if(i<add.getContents().size()-1){
                buffer.append(",");
            }
        }
        question.setCtime(new Date());
        question.setNums(0);
        question.setContent(buffer.toString());

        return questionMapper.insert(question)>0?AjaxResult.success():AjaxResult.error("亲，新增问卷失败！");
    }

    @Override
    public AjaxResult update(Question question) {
        question.setUtime(new Date());
        return questionMapper.updateById(question)>0?AjaxResult.success():AjaxResult.error("亲，修改问卷失败");
    }

    @Override
    public AjaxResult delete(Long[] quetionIds) {

        return questionMapper.deleteBatchIds(Arrays.stream(quetionIds).collect(Collectors.toList()))>0?AjaxResult.success():AjaxResult.error("亲，删除问卷失败");
    }

    @Override
    public List<Question> queryPage(QuestionQuery query) {
        LambdaQueryWrapper<Question> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Question::getCtime);
        if(StringUtils.hasLength(query.getTitle())){
            wrapper.like(Question::getTitle,query.getTitle());
        }
        if(query.getSdate()!=null){
            wrapper.ge(Question::getSdate,query.getSdate());
        }
        if(query.getEdate()!=null){
            wrapper.le(Question::getEdate,query.getEdate());
        }
        List<Question> questionList = questionMapper.selectList(wrapper);
        return questionList;
    }

    @Override
    public AjaxResult deleteBatch(List<Integer> ids) {
        return questionMapper.deleteBatchIds(ids)>0?AjaxResult.success():AjaxResult.error("亲，批量删除失败！");
    }

    @Override
    public AjaxResult upload(MultipartFile file) throws IOException {
        //1.验证
        if(file.isEmpty()){
            return AjaxResult.error("请上传资源");
        }
        String obj=minioUtil.rename(file.getOriginalFilename());
        //2.上传
        String url=minioUtil.uploadFile(obj,file.getBytes(),file.getContentType());
        if(url!=null){
            //上传成功
            return AjaxResult.success(url);
        }
        return AjaxResult.error("亲，上传资源失败！");
    }
}

